package com.example.secondhandmall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.secondhandmall.R;
import com.example.secondhandmall.entity.GoodsEntity;
import com.example.secondhandmall.entity.SearchGoodsEntity;
import com.example.secondhandmall.view.CircleCornerTransForm;
import com.example.secondhandmall.view.CircleTransform;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.List;

public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<SearchGoodsEntity> datas;

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setDatas(List<SearchGoodsEntity> datas) {
        this.datas = datas;
    }

    public SearchAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public SearchAdapter(Context mContext, List<SearchGoodsEntity> datas) {
        this.mContext = mContext;
        this.datas = datas;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_goods_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    //给控件对象赋值，给ViewHoleder绑定数据
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        SearchGoodsEntity searchGoodsEntity = datas.get(position);
        viewHolder.tvIitle.setText(searchGoodsEntity.getGoodsName());
        viewHolder.tvPrice.setText(searchGoodsEntity.getGoodsPrice()+"");
        viewHolder.tvName.setText(searchGoodsEntity.getUserName());
//        viewHolder.goodsEntity = goodsEntity;
        viewHolder.id = searchGoodsEntity.getGoodsId();

        Picasso.with(mContext)
                .load(searchGoodsEntity.getUrl().getGoodsPhotoUrl())
                .transform(new CircleCornerTransForm())
                .into(viewHolder.ivImg);
        Picasso.with(mContext)
                .load(searchGoodsEntity.getUserPhoto())
                .transform(new CircleTransform())
                .into(viewHolder.ivAvatar);
    }

    @Override
    public int getItemCount() {
        if(datas!=null&&datas.size()>0){
            return datas.size();
        }
        return 0;
    }

    //绑定控件，得到控件对象
    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivImg;
        private TextView tvIitle;
        private TextView tvPrice;
//        private TextView tvDetail;

        private ImageView ivAvatar;
        private TextView tvName;

//        private GoodsEntity goodsEntity;
        private int id;

        public ViewHolder(@NonNull View view) {
            super(view);
            ivImg = view.findViewById(R.id.iv_img);
            tvIitle = view.findViewById(R.id.tv_title);
            tvPrice = view.findViewById(R.id.tv_price);
//            tvDetail=view.findViewById(R.id.iv_img);
            ivAvatar = view.findViewById(R.id.iv_avatar);
            tvName = view.findViewById(R.id.tv_name);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(id);
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int id);
    }
}
